package ssm.blog.controller.admin;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import ssm.blog.entity.BlogType;
import ssm.blog.service.BlogTypeService;
import ssm.blog.util.ResponseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: lyon
 * @description: 博客类别控制层
 * @author: Lyon
 * @create: 2018-05-30 13:09
 **/
@Controller
@RequestMapping("/admin")
public class AdminBlogTypeController {
    @Resource
    BlogTypeService blogTypeService;
    @RequestMapping("/blogTypeAdd")
    String blogTypeAdd(){
        return "interface/admin/blogTypeAdd";
    }
    @RequestMapping("/blogTypeEdit")
    String blogTypeEdit(Model model, Integer id){
        BlogType blogType=blogTypeService.selectById(id);
        model.addAttribute("blogType",blogType);
        return "interface/admin/blogTypeEdit";
    }
    /**
    * @Description:  添加博客分类
    * @Param: [blogType, response]
    * @return: java.lang.String
    * @Author: Lyon
    * @Date: 2018/5/30
    */
    @RequestMapping("/addBlogType")
    String addBlogTypeAdd(BlogType blogType, HttpServletResponse response) throws Exception {
        JSONObject result=new JSONObject();
        int statu=blogTypeService.addBlogType(blogType);
        if(statu>0){
            result.put("success",true);
        }else{
            result.put("success",false);
        }
        ResponseUtil.write(response,result);
        return null;
    }
    /**
    * @Description:  单个删除博客分类
    * @Param: [response, id]
    * @return: java.lang.String
    * @Author: Lyon
    * @Date: 2018/5/30
    */
    @RequestMapping("/blogTypeDele")
    String blogTypeDele(HttpServletResponse response,Integer id) throws Exception {
        JSONObject result=new JSONObject();
        try{
            int resultRows=blogTypeService.dele(id);
            if(resultRows>0){
                result.put("success",true);
            }else {
                result.put("success",false);
            }
        }catch (Exception e){
            result.put("success",false);
        }
        ResponseUtil.write(response,result);
        return null;
    }
    /**
    * @Description:  批量删除博客分类
    * @Param: [response, ids]
    * @return: java.lang.String
    * @Author: Lyon
    * @Date: 2018/5/30
    */
    @RequestMapping("blogTypesDele")
    String blogTypesDele(HttpServletResponse response,String ids)throws Exception{
        //分割得到所有ID
        String[] IDS_str=ids.split(",");
        //转化为Integer数组
        List<Integer> IDS_INT=new ArrayList<>();
        for(String i:IDS_str) {
            IDS_INT.add(Integer.valueOf(i));
        }
        JSONObject result=new JSONObject();
        //删除
        try {
            int resultRows=blogTypeService.deles(IDS_INT);
            //返回删除结果
            if(resultRows>0){
                result.put("success",true);
            }else {
                result.put("success",false);
            }
        }catch (Exception e){
            result.put("success",false);
        }
        ResponseUtil.write(response,result);
        return null;
    }
    /**
    * @Description:  修改博客分类
    * @Param: [blogType]
    * @return: java.lang.String
    * @Author: Lyon
    * @Date: 2018/5/30
    */
    @RequestMapping("/updateBlogType")
    String updateBlogType(HttpServletResponse response,BlogType blogType) throws Exception {
        int resultRows=blogTypeService.updateByIdSelective(blogType);
        JSONObject result=new JSONObject();
        if(resultRows>0){
            result.put("success",true);
        }else {
            result.put("success",false);
        }
        ResponseUtil.write(response,result);
        return null;
    }
}
